package org.example.etl;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ClassName WebLogMapper
 * @Author 小坏
 * @Date 2021/8/2、10:48
 * @Version 1.0
 */
public class WebLogMapper extends Mapper<LongWritable, Text, Text, NullWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //1获取一行
        String line = value.toString();

        //2ETL
        boolean result = parseLog(line, context);
        if (!result) {
            return;
        }

        //3写出
        context.write(value, NullWritable.get());

    }

    private boolean parseLog(String line, Context context) {
        //58.215.204.118 - - [18/Sep/2013:06:51:41 +0000] "-" 400 0 "-" "-"
        //切割
        String[] split = line.split(" ");

        //判断日志长度是否大于十一
        if (split.length > 11) {
            return true;
        } else {
            return false;
        }
    }
}
